//
//  YDStockModeController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/3/14.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDStockModeController.h"
#import <WebKit/WebKit.h>

@interface YDStockModeController ()<WKNavigationDelegate>

@property (nonatomic , weak)UIProgressView *progressView;
@property (nonatomic , weak) WKWebView *webView;


@property (nonatomic , weak) UIScrollView *scrollerView;

@property (nonatomic , weak) UIImageView *imageView;

@end

@implementation YDStockModeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self progressView];
    [self addWebView];
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    
}
- (UIProgressView *)progressView
{
    if (!_progressView){
        UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, 0)];
        progressView.tintColor = [UIColor colorWithHexString:mainGray];
        progressView.trackTintColor = [UIColor colorWithHexString:mainGray];
        [self.view addSubview:progressView];
        self.progressView = progressView;
    }
    return _progressView;
}

-(void)addWebView
{
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    webView.backgroundColor = [UIColor whiteColor];
    webView.navigationDelegate = self;
    webView.opaque = NO;
    webView.multipleTouchEnabled = YES;
    [self.view insertSubview:webView belowSubview:_progressView];
    
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    NSURL *baseURL = [NSURL URLWithString:@"http://waben.cn/buy/buy.html"];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
    
    [webView loadRequest:request];
    
    self.webView = webView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"])
    {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1)
        {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }
        else
        {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}










//- (void)viewDidLoad
//{
//    [super viewDidLoad];
//    self.extendedLayoutIncludesOpaqueBars = YES;
//    self.view.backgroundColor = [UIColor whiteColor];
//    [self scrollerView];
//    [self imageView];
//}

-(CGSize)caluteImageWH
{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stockAnliImage" ofType:@"png"] ;
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    //计算宽高，按比例缩放
    //宽度固定，均为屏幕宽度
    CGFloat contentSizeW = LKScreenW;
    CGFloat contentSizeH = (contentSizeW / image.size.width) * image.size.height;
    return CGSizeMake(contentSizeW, contentSizeH);
}


-(UIScrollView *)scrollerView
{
    if (_scrollerView == nil) {
        UIScrollView *scrollerView = [[UIScrollView alloc] init];
        [self.view addSubview:scrollerView];
        scrollerView.backgroundColor = [UIColor clearColor];
        scrollerView.bounces = YES;
        scrollerView.showsVerticalScrollIndicator = NO;
        scrollerView.showsHorizontalScrollIndicator = NO;
        [scrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(self.view);
        }];
        CGSize  size = [self caluteImageWH];
        scrollerView.contentSize = CGSizeMake(size.width, size.height);
        
        _scrollerView = scrollerView;
    }
    return _scrollerView;
}


-(UIImageView *)imageView
{
    if (_imageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.scrollerView addSubview:imageView];
        CGSize  size = [self caluteImageWH];
        imageView.frame = CGRectMake(0, 0, size.width, size.height);
        
        
        imageView.backgroundColor = [UIColor clearColor];
        imageView.contentMode = UIViewContentModeScaleToFill;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"stockAnliImage" ofType:@"png"] ;
        imageView.image = [UIImage imageWithContentsOfFile:path];
        
        _imageView = imageView;
        
    }
    return _imageView;
}


@end
